package com.example.administrator.managesystem.fragment.firstpage;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.activity.firstpage.wiki.WikiDetailActivity;
import com.example.administrator.managesystem.adapter.WikiAdapter;
import com.example.administrator.managesystem.adapter.holder.WikiHolder;
import com.example.administrator.managesystem.base.BaseFragment;
import com.example.administrator.managesystem.base.BaseHelper;
import com.example.administrator.managesystem.bean.WikiDetailBean;
import com.example.administrator.managesystem.bean.WikiListBean;
import com.example.administrator.managesystem.presenter.firstpage.FirstPagePresenter;
import com.example.administrator.managesystem.ui.WikiView;
import com.example.administrator.managesystem.utils.ShareUtil;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import cn.bingoogolapple.bgabanner.BGABanner;
public class WikiFragment extends BaseFragment implements WikiView {
    private BGABanner bgaBanner;
    private ArrayList<Integer> bannerBeanArrayList = new ArrayList<>();

    // 定义
    private XRecyclerView mRecyclerView;
    private WikiAdapter myXrecyclerAdapter;
    private FirstPagePresenter firstPagePresenter;
    private ArrayList<WikiListBean.DataBean> superList=new ArrayList<>();

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        mRecyclerView=view.findViewById(R.id.recyclerview);
        View header =LayoutInflater.from(getActivity()).inflate(R.layout.wiki_page_header,null);
        bgaBanner =  header.findViewById(R.id.banner_splash_pager);
        mRecyclerView.addHeaderView(header);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_wiki;
    }

    @Override
    protected void doBusiness() {
        firstPagePresenter=new FirstPagePresenter(this);
        myXrecyclerAdapter = new WikiAdapter(getActivity(),superList);
        firstPagePresenter.wikiListRequest(20,null,0,getActivity());
        BaseHelper.setLinearLayoutManagerVertical(getActivity(),mRecyclerView,myXrecyclerAdapter);
        myXrecyclerAdapter.addListener(new WikiHolder.OnItemClickListener() {
            @Override
            public void onItemClick(int position, String wiki_id, View v) {
                firstPagePresenter.wikiDetailRequest(ShareUtil.getUserId(getActivity()),wiki_id,getActivity());
            }
        });

        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                superList.clear();
                firstPagePresenter.wikiListRequest(20,null,0,getActivity());
                mRecyclerView.refreshComplete();

            }

            @Override
            public void onLoadMore() {

               firstPagePresenter.wikiListRequest(20,superList.get(superList.size()-1).getRelease_time(),1,getActivity());
                mRecyclerView.loadMoreComplete();
            }
        });
        bannerBeanArrayList.add(R.mipmap.banner1);
        bannerBeanArrayList.add(R.mipmap.banner2);

        bgaBanner.setData(bannerBeanArrayList, null);
        bgaBanner.setAdapter(new BGABanner.Adapter<ImageView, Integer>() {
            @Override
            public void fillBannerItem(BGABanner banner, ImageView itemView, Integer model, int position) {
                Picasso.with(getActivity())
                        .load(model)
                        .placeholder(R.mipmap.banner1)
                        .error(R.mipmap.banner1)
                        .into(itemView);
            }
        });

    }

    @Override
    public void showWikiList(final List<WikiListBean.DataBean> list, int type) {
        if(list.isEmpty()){
           // stateLayout.showEmptyView();
            return;
        }
        if(type==1){
            superList.addAll(list);
        }else{

            superList.clear();
            superList.addAll(list);
        }
    myXrecyclerAdapter.notifyDataSetChanged();




    }

    @Override
    public void showWikiDetail(WikiDetailBean.DataBean dataBean) {
        Intent intent=new Intent(getActivity(),WikiDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("dataBean", dataBean);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void updateCollectUI() {

    }

    @Override
    public void updateRepost(int type) {

    }


}